<template>
  <div class="article">
    <el-card>
      <template #header>
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="'/layout'">首页</el-breadcrumb-item>
          <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        </el-breadcrumb>
      </template>
      <el-form label-width="100px">
        <el-form-item label="状态">
          <!-- 单选框组件 -->
          <el-radio-group v-model="form.status">
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
            <el-radio :label="4">已删除</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- 频道 -->
        <el-form-item label="频道">
          <!-- {{ form.channel_id }} -->
          <HmSelect :form="form"></HmSelect>
          <!-- <el-select placeholder="请选择" v-model="form.channel_id">
            <el-option
              v-for="item in channelsList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select> -->
        </el-form-item>
        <!-- 日期 -->
        <el-form-item label="日期">
          <el-date-picker
            v-model="form.picker"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item>
          <el-button type="primary" @click="submit">筛选</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <template #header> 根据筛选条件共查询到{{ total }}条结果： </template>
      <el-table :data="list">
        <el-table-column label="封面">
          <template v-slot="scope">
            <img class="cover" :src="scope.row.cover.images[0]" alt="" />
          </template>
        </el-table-column>
        <el-table-column label="标题" prop="title"></el-table-column>
        <el-table-column label="状态">
          <template v-slot="{ row }">
            {{ row.status === 2 ? '审核通过' : '审核失败' }}
          </template>
        </el-table-column>
        <el-table-column
          label="发布时间"
          prop="pubdate"
          width="200"
        ></el-table-column>
        <el-table-column label="阅读数" prop="read_count"></el-table-column>
        <el-table-column label="评论数" prop="comment_count"></el-table-column>
        <el-table-column label="点赞数" prop="like_count"></el-table-column>
        <el-table-column label="操作" width="200">
          <template v-slot="scope">
            <div>
              {{ scope.$index }}
              <el-button @click="edit(scope.row.id)">编辑</el-button>
              <el-button @click="del(scope.row.id)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        class="pagination"
        :total="total"
        :current-page="form.page"
        :page-size="form.per_page"
        :page-sizes="[1, 10, 20, 30, 40]"
        layout="total,sizes,prev,pager,next,jumper"
        @size-change="sizeChange"
        @current-change="currentChange"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import { mpArticles, mpArticlesDelete } from '@/api/index.js'
export default {
  data () {
    return {
      form: {
        picker: [], // [起始时间,结束时间]
        status: null, // 否文章状态，0-草稿，1-待审核，2-审核通过，3-审核失败，不传为全部
        channel_id: '', // 是,不传为全部
        begin_pubdate: '', // 否,起始时间
        end_pubdate: '', // 否,截止时间
        page: 1, // 否,页码,默认为1页
        per_page: 10 // 否每页数量,不传为默认10
      },
      // channelsList: [], // 获取的频道
      list: [], // 获取的文章列表
      total: 0
    }
  },
  async created () {
    // 进入页面,调用获取频道接口
    // const res = await channels()
    // console.log('获取频道res:', res)
    // this.channelsList = res.data.channels

    // 以上的在HmSelect组件封装了
    // 进入页面获取文章列表
    this.getData()
  },

  methods: {
    // 封装获取文章列表函数
    async getData () {
      const res = await mpArticles(this.form)
      console.log('获取文章列表res:', res)
      this.list = res.data.results
      this.total = res.data.total_count
    },

    // 点击筛选获取文章列表
    // 点击搜索回到第一页,页容量改变回到第一页
    async submit () {
      this.form.page = 1
      this.getData()
    },

    // 页容量改变
    sizeChange (size) {
      this.form.per_page = size
      this.form.page = 1
      this.getData()
    },

    // 当前页改变
    currentChange (page) {
      this.form.page = page
      this.getData()
    },

    // 点击删除
    del (id) {
      this.$confirm('您确认删除该数据吗', '温馨提示', {
        confirmButtonText: '确认删除',
        cancelButtonText: '取消删除',
        type: 'warning'
      })
        .then(async () => {
          await mpArticlesDelete(id)
          this.$message.success('删除成功')
          //  跳转第一页,刷新页面
          this.submit()
        })
        .catch(() => {})
    },
    // 编辑
    edit (id) {
      this.$router.push('/layout/publish?id=' + id)
    }
  },

  watch: {
    // 默认只能侦听栈的变化,如果堆与栈都想侦听deep:true
    'form.picker': {
      handler (newVal, oldVal) {
        console.log('值改变了:', newVal)
        if (newVal.length === 2) {
          this.form.begin_pubdate = newVal[0]
          this.form.end_pubdate = newVal[1]
        }
      },
      deep: true
    }
  }
}
</script>

<style lang="less" scoped>
.article {
  .cover {
    width: 50px;
  }
  .el-date-editor {
    width: 420px;
  }
  .pagination {
    text-align: center;
    padding-top: 20px;
  }
}
</style>
